package myJs.implicits

import myJs.myPkg.rSelect._
import org.scalajs.dom.{Element, HTMLFormElement}
import slinky.readwrite.ObjectOrWritten

import scala.scalajs.js
import scala.scalajs.js.|
import upickle.default._


trait MyFieldValueTool {
  type FieldValue = js.UndefOr[String | SelectOption]

  implicit class MyJsAny(v: FieldValue) {

    def toSelectionOption = {
      if (v == null || v.isEmpty) {
        js.undefined
      } else {
        val selection = v match {
          case x if x.isInstanceOf[SelectOption] =>
            js.defined(x.asInstanceOf[SelectOption])
          case _ =>
            v.asInstanceOf[SelectOptionT].toSelectionOption
        }
        selection
      }
    }

  }

  implicit class MySelectionOptionT(v: SelectOptionT) {

    def toSelectionOption = {
      v.value.toOption match {
        case Some(x) =>
          js.defined(SelectOption(value = v.value.get, label = v.label.get))
        case None => js.undefined
      }

    }

  }


}
